home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / serial / statseri.000 / statseri / statserial-1.1 / statserial.c < prev   
C/C++ Source or Header  |  1994-12-17  |  6KB  |  196 lines

  1. /*
  2.  *        statserial - Serial Port Status Utility
  3.  *
  4.  * Copyright (C) 1994 Jeff Tranter (Jeff_Tranter@Mitel.COM)
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  ********************************************************************
  21.  *
  22.  * See the man page for a description of what this program does and what
  23.  * the requirements to run it are.
  24.  * 
  25.  * It was developed using:
  26.  * - Linux kernel 1.1.73
  27.  * - gcc 2.5.8
  28.  * - no-name serial card (16450), FIFO card (16550)
  29.  * 
  30.  * Jeff Tranter (Jeff_Tranter@Mitel.COM)
  31.  * Frank Baumgart (godot@uni-paderborn.de)
  32.  */
  33.  
  34. #include <curses.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <unistd.h>
  38. #include <fcntl.h>
  39. #include <termios.h>
  40. #include <string.h>
  41. #include <sys/ioctl.h>
  42.  
  43. /* global variables */
  44. char device[255];                          /* name of device to open */
  45. const char *defaultDevice = "/dev/cua1";   /* default device if none specified on command line */
  46. int n_option = 0;                          /* set if -n option is used */
  47. int d_option = 0;                          /* set if -d option is used */
  48. int x_option = 0;                          /* set if -x option is used */
  49.  
  50. /* print command usage and exit */
  51. void usage()
  52. {
  53.   fprintf(stderr, "usage: statserial [-n | -d | -x] [device]\n");
  54.   fprintf(stderr, "  -n option disables looping\n");
  55.   fprintf(stderr, "  -d option prints modem status as a decimal number\n");
  56.   fprintf(stderr, "  -x option prints modem status as a hexadecimal number\n");
  57.   fprintf(stderr, "  Default device is %s\n", device);
  58.   exit(1);
  59. }
  60.  
  61. /* handle command line options */
  62. void parse_args(int argc, char **argv)
  63. {
  64.   const char     *flags = "ndx";
  65.   int             c;
  66.   
  67.   while ((c = getopt(argc, argv, flags)) != EOF) {
  68.     switch (c) {
  69.     case 'n':
  70.       n_option = 1;
  71.       break;
  72.     case 'd':
  73.       d_option = 1;
  74.       break;
  75.     case 'x':
  76.       x_option = 1;
  77.       break;
  78.     case '?':
  79.       usage();
  80.       break;
  81.     }
  82.   }
  83.     
  84.   if (n_option + d_option + x_option > 1) {
  85.     printf("statserial: options are mutually exclusive\n");
  86.     usage();
  87.   }
  88.     
  89.   /* check for a single additional argument */
  90.   if ((argc - optind) > 1)
  91.     usage(); /* too many arguments */
  92.   
  93.   if ((argc - optind) == 1)
  94.     strcpy(device, argv[optind]); /* one argument */
  95. }
  96.  
  97. /* called before exiting */
  98. void cleanup()
  99. {
  100.   endwin(); /* required by curses */
  101. }
  102.  
  103. /* main program */  
  104. int main(int argc, char *argv[])
  105. {
  106.   int fd;                       /* for serial device */
  107.   int status;                   /* status of system calls */
  108.   unsigned int old_status = 0;  /* value of previous call */
  109.   unsigned int arg;             /* value returned by ioctl */
  110.  
  111.   /* make default the default, use $MODEM if set */
  112.   if (getenv("MODEM"))
  113.     strcpy(device, getenv("MODEM"));
  114.   else
  115.     strcpy(device, defaultDevice);
  116.  
  117.   /* parse command line arguments */
  118.   parse_args(argc, argv);
  119.  
  120.   /* open port */
  121.   fd = open(device, O_RDONLY);
  122.   if (fd == -1) {
  123.     char s[255];
  124.     sprintf(s, "statserial: can't open device `%s'", device);
  125.     perror(s);
  126.     exit(1);
  127.   }
  128.  
  129.   /* init curses */
  130.   if (!d_option && !x_option) {
  131.     initscr();
  132.     atexit(cleanup);
  133.   }
  134.  
  135.   /* loop forever */
  136.   for (;;) {
  137.  
  138.     /* get modem status info */  
  139.     status = ioctl(fd, TIOCMGET, &arg);
  140.     if (status != 0) {
  141.       perror("statserial: TIOCMGET failed");
  142.       exit(1);
  143.     }
  144.  
  145.     /* avoid unneccessary screen updates */
  146.     if (arg == old_status)
  147.     {
  148.       sleep(1);
  149.       continue;
  150.     }
  151.     old_status = arg;
  152.  
  153.     /* home cursor */
  154.     if (!d_option && !x_option) {
  155.       move(0,0);
  156.     }
  157.  
  158.     /* print status in decimal */
  159.     if (d_option) {
  160.       printf("%d\n", arg);
  161.       exit(0);
  162.     }
  163.  
  164.     /* print status in hex */
  165.     if (x_option) {
  166.       printf("%x\n", arg);
  167.       exit(0);
  168.     }
  169.  
  170.     printw("Device: %s\n\n", device);
  171.     printw("Signal  Pin  Pin  Direction  Status  Full\n");
  172.     printw("Name    (25) (9)  (computer)         Name\n");
  173.     printw("-----   ---  ---  ---------  ------  -----\n");
  174.     printw("FG       1    -      -           -   Frame Ground\n");
  175.     printw("TxD      2    3      out         -   Transmit Data\n");
  176.     printw("RxD      3    2      in          -   Receive  Data\n");
  177.     printw("RTS      4    7      out         %1d   Request To Send\n", !!(arg & TIOCM_RTS));
  178.     printw("CTS      5    8      in          %1d   Clear To Send\n", !!(arg & TIOCM_CTS));
  179.     printw("DSR      6    6      in          %1d   Data Set Ready\n", !!(arg & TIOCM_DSR));
  180.     printw("GND      7    5      -           -   Signal Ground\n");
  181.     printw("DCD      8    1      in          %1d   Data Carrier Detect\n", !!(arg & TIOCM_CAR));
  182.     printw("DTR     20    4      out         %1d   Data Terminal Ready\n", !!(arg & TIOCM_DTR));
  183.     printw("RI      22    9      in          %1d   Ring Indicator\n", !!(arg & TIOCM_RNG));
  184.     refresh();
  185.  
  186.     /* break out if -n option was used */
  187.     if (n_option)
  188.       exit(0);
  189.  
  190.     /* delay 1 second between loops */
  191.     sleep(1);
  192.   }
  193.   
  194.   return 0;
  195. }
  196.